import cv2
from matplotlib import pyplot as plt


img1=cv2.imread(r'C:\Users\ASUS\Desktop\pic2.jpg',0) #读取图片
img2=cv2.imread(r'C:\Users\ASUS\Desktop\pic6.jpg',0)

_,thre1=cv2.threshold(img1,0,0xff,cv2.THRESH_OTSU) #大津算法
_,thre2=cv2.threshold(img2,0,0xff,cv2.THRESH_OTSU) #大津算法

hist1=cv2.calcHist([img1],[0],None,[256],[0,256])#计算灰度图
hist2=cv2.calcHist([img2],[0],None,[256],[0,256])#计算灰度图

cv2.imshow('thre1',thre1)
cv2.imshow('thre2',thre2)

plt.subplot(211),plt.plot(hist1)
plt.subplot(212),plt.plot(hist2)
plt.show()

cv2.waitKey()
cv2.destroyAllWindows()